﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCoffeeShop.DataTransferObjects;
using MvcCoffeeShop.BusinessLogic;
using MvcCoffeeShop.Models;
using MvcCoffeeShop;

namespace MvcCoffeeShop.Views.ShoppingCart
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/

        enum CartActionType
        {
            AddToCart = 1,
            RemoveFromCart = 2
        }

        

        public string getCartID()
        {
            //Create a new guid that will serve
            //as the cart item GUID
            Guid itemGuid;
            itemGuid = Guid.NewGuid();
            return itemGuid.ToString();
        }

        [HttpGet]
        public ActionResult Index()
        {

          List<CartItemDTO> oCartList;
          oCartList = new List<CartItemDTO>();
          oCartList = (List<CartItemDTO>)Session[SharedConstants.cSession_MvcShoppingCart];

          return View(oCartList);
                        
        }

        [HttpGet]
        public ActionResult PendingCartItem(int productid)
        {
            var oProduct = new ProductDTO();
            ProductModels oProductModel = new ProductModels();
            oProduct = oProductModel.GetProduct((int)productid);

            //TODO: IOrtiz - Make this more dynamic so that each product can have it's own max quantity
            var oMaximumQuantity = new List<string>();

            for(int i = 1; i < 21; i++)
            {
                oMaximumQuantity.Add(i.ToString());
            }

            ViewBag.MaximumQuantity = new SelectList(oMaximumQuantity);
            ViewBag.ProductID = productid;

            

            return View(oProduct);
        }
       
        [HttpPost]
        public JsonResult PendingCartItem(int productid, int quantity)
        {
            List<CartItemDTO> oCartList; 
            oCartList = new List<CartItemDTO>();
            CartItemDTO oCartItem = new CartItemDTO();

            //If the cart is not empty then we want to get all of the items that 
            //are stored in session.
            oCartList = (List<CartItemDTO>)Session[SharedConstants.cSession_MvcShoppingCart];


            if (oCartList == null)
                    {
                        try
                        {
                            //IOrtiz - If there is nothing in the cart then we need to add
                            //a brand new item to the cart.
                            oCartList = new List<CartItemDTO>();
                            var oProduct = new ProductDTO();
                            ProductModels oProductModel = new ProductModels();

                            oProduct = oProductModel.GetProduct((int)productid);

                            oCartItem.Quantity = 0;
                            oCartItem.ProductID = productid;
                            oCartItem.ProductName = oProduct.ProductName;
                            oCartItem.PicturePath = oProduct.PicturePath;
                            oCartItem.Quantity += quantity;
                            oCartItem.UnitPrice = oProduct.UnitPrice;
                            oCartItem.ShoppingCartItemID = getCartID();

                            oCartList.Add(oCartItem);
                            //Populate session with the latest list.
                            Session[SharedConstants.cSession_MvcShoppingCartItemsCount] = oCartList.Count();
                            Session[SharedConstants.cSession_MvcShoppingCart] = oCartList;
                        }
                        catch 
                        {
                            return Json(new { result = false });
                        }
                       
                    }
            else
                    {
                        //IOrtiz - At this point we have determined that there is
                        //         one or more items in the cart.

                        try
                        {

                            //IOrtiz - Check to see if the product already exists in the cart.
                            
                            var intProductIDExists = (from row in oCartList
                                                      where row.ProductID == productid
                                                      select row).Count();

                            //IOrtiz - If it exists, then we simply want to update the quantity for
                            //         for that product.
                            if (intProductIDExists > 0)
                            {

                                //IOrtiz - Locate the unique cart id and update the cart item with the new
                                //         quantity
                                string strExistingCartID = (from row in oCartList
                                                            where row.ProductID == productid
                                                            select row.ShoppingCartItemID).SingleOrDefault();

                                foreach (CartItemDTO myCartItem in new List<CartItemDTO>(oCartList))
                                {
                                    if (myCartItem.ShoppingCartItemID.Trim().ToUpper() == strExistingCartID.Trim().ToUpper())
                                    {
                                        myCartItem.Quantity += quantity;
                                        break;
                                    }
                                }

                                //Populate session with the latest list.
                                Session[SharedConstants.cSession_MvcShoppingCartItemsCount] = oCartList.Count();
                                Session[SharedConstants.cSession_MvcShoppingCart] = oCartList;

                            }
                            else
                            {

                                //IOrtiz - This product has not been added to the cart so we will need to add it as 
                                //         a new cart item.

                                var oProduct = new ProductDTO();
                                ProductModels oProductModel = new ProductModels();

                                oProduct = oProductModel.GetProduct((int)productid);

                                oCartItem.Quantity = 0;
                                oCartItem.ProductID = productid;
                                oCartItem.ProductName = oProduct.ProductName;
                                oCartItem.PicturePath = oProduct.PicturePath;
                                oCartItem.Quantity += quantity;
                                oCartItem.UnitPrice = oProduct.UnitPrice;
                                oCartItem.ShoppingCartItemID = getCartID();

                                oCartList.Add(oCartItem);

                                //Populate session with the latest list.
                                Session[SharedConstants.cSession_MvcShoppingCartItemsCount] = oCartList.Count();
                                Session[SharedConstants.cSession_MvcShoppingCart] = oCartList;

                            }

                        }
                        catch
                        {
                            return Json(new { result = false });
                        }

                    }

            return Json(new { result = true });
            
        }

       
        public ActionResult removeFromCart(string cartitemid)
        {

                List<CartItemDTO> oCartList;
                oCartList = new List<CartItemDTO>();

                //If the cart is not empty then we want to get all of the items that 
                //are stored in session.
                oCartList = (List<CartItemDTO>)Session[SharedConstants.cSession_MvcShoppingCart];

                foreach (CartItemDTO myCartItem in new List<CartItemDTO>(oCartList))
                {
                    if (myCartItem.ShoppingCartItemID.Trim().ToUpper() == cartitemid.Trim().ToUpper())
                    {
                        oCartList.Remove(myCartItem);
                    }
                }

                //Populate session with the latest list.
                Session[SharedConstants.cSession_MvcShoppingCartItemsCount] = oCartList.Count();
                Session[SharedConstants.cSession_MvcShoppingCart] = oCartList;

                return RedirectToAction("Index");

        }

        public ActionResult CheckOut()
        {
            List<CartItemDTO> oCartList;
            oCartList = new List<CartItemDTO>();
            CartItemDTO oCartItem = new CartItemDTO();

            //If the cart is not empty then we want to get all of the items that 
            //are stored in session.
            oCartList = (List<CartItemDTO>)Session[SharedConstants.cSession_MvcShoppingCart];

            double dblTotalPrice = 0;

            foreach (CartItemDTO myCartItem in new List<CartItemDTO>(oCartList))
            {
                dblTotalPrice += (double)((int)myCartItem.Quantity * (double)myCartItem.UnitPrice);
            }
                        
            ViewBag.TotalPrice = dblTotalPrice;

            return View(oCartList);
        }

        [Authorize]
        public ActionResult PayNow()
        {
            return View();
        }

    }
}
